/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package com.isisufes.joinus.appserver;

import java.util.*;

import javax.crypto.*;

public class Auth
{
    static Map<String, SecretKey> keys = null;

    static public void init()
    {
        if (keys != null) return;

        keys = new HashMap<String, SecretKey>();
    }

    static public String register(String id)
        throws Exception
    {
        KeyGenerator keygen = KeyGenerator.getInstance("DES");
        SecretKey key = keygen.generateKey();

        keys.put(id, key);

        return Base64.encode(key.getEncoded());
    }

    static public Map<String,String> decode(String id, String msg)
        throws Exception
    {
        SecretKey key = keys.get(id);

        if (key == null) throw new Exception("Not logged");

        return Crypter.decrypt(key, msg);
    }
}
